package main

import (
	"fmt"
	"time"
)

//冒泡排序 比较慢
func bubble1(n []int) []int {
	for i := 0; i < len(n)-1; i++ {
		flag := true
		for j := 0; j < len(n)-1; j++ {
			if n[j] > n[j+1] {
				n[j], n[j+1] = n[j+1], n[j]
				flag = false
			}
		}
		if flag == true { //如果标志符未改变 说明已经排序好 直接返回
			return n
		}
	}
	return n
}

//更快
func bubble2(n []int) []int {
	for i := 0; i < len(n)-1; i++ {
		for j := i + 1; j < len(n); j++ {
			if n[i] > n[j] {
				n[i], n[j] = n[j], n[i]
			}
		}
	}
	return n
}

func main() {
	num := []int{2, 4, 6, 1, 4}
	start1 := time.Now()
	res1 := bubble1(num)
	stop1 := time.Since(start1)

	start2 := time.Now()
	res2 := bubble1(num)
	stop2 := time.Since(start2)
	fmt.Printf("res:%v,time:%d\n", res1, stop1)
	fmt.Printf("res:%v,time:%d\n", res2, stop2)
}
